* ASI 87 88 cleanup
* Unlike process88-ach.do, it keeps asicode88 and does not merge into asicode99


use "$data\asi8788.dta", clear
keep v10 v3 v16 v17 v18 v19 v20 v24 v26 v28 v29 v35 v37 v38 v39 v40 v88 v41 v42 v50 v51 v49 v52 v66 v67 v68 v57 v58 v61 v93 v108

g year = 1987

rename v24 ruralurbancode
rename v10 industrycode
rename v3 statecode
rename v16 ownershipcode
rename v17 orgcode
rename v18 managcode
rename v19 schemecode
rename v20 districtcode
rename v26 setup
rename v28 openclose
rename v29 powercode
rename v35 factories
rename v37 closingvalueland
rename v38 fixedcapitalop
rename v39 fixedcapitalcl
rename v40 landrental
rename v41 totalrent
rename v42 totalassets
rename v50 workers
rename v51 workers_2
rename v49 mandays
rename v88 workerswages
rename v66 totaloutput
rename v67 depreciation
rename v108 electricity
rename v57 fuel
rename v58 materials
rename v61 totalinput
rename v68 valueadded
rename v93 profits


drop if openclose==1

/* for 87-88 all variables have already been multiplied so we demultiply*/
ge multiplier=1
replace multiplier=1 if schemecode==1|schemecode==2|schemecode==3|schemecode==6|schemecode==7
replace multiplier=3 if schemecode==5|schemecode==9
tab multiplier


replace workers=workers/multiplier
replace workers_2=workers_2/multiplier
replace totaloutput=totaloutput/multiplier
replace fixedcapitalcl=fixedcapitalcl/multiplier
replace workerswages=workerswages/multiplier
replace mandays=mandays/multiplier
replace valueadded=valueadded/multiplier
replace totalassets=totalassets/multiplier
replace profits=profits/multiplier

/* mapping old industry codes to new ones*/
ge indcode_str=string(industrycode)
replace indcode_str=substr(indcode_str, 1,3)
destring indcode_str, generate(indcode_3dig)
drop indcode_str
label var indcode_3dig "3 dig industry code"

gen code=indcode_3dig
do "$do\changingcodes7087.do"

drop if indcode_3dig>=400
drop if indcode_3dig<200

rename code nic87code

/* creating a 2-digit code */
ge nic87code_str=string(nic87code)
replace nic87code_str=substr(nic87code_str, 1,2)
destring nic87code_str, generate(nic87code_2dig)
drop nic87code_str
label var nic87code_2dig "2 dig NIC87 code"

/* creating a 1-digit code */
ge nic87code_str=string(nic87code)
replace nic87code_str=substr(nic87code_str, 1,1)
destring nic87code_str, generate(nic87code_1dig)
drop nic87code_str
label var nic87code_1dig "1 dig NIC87 code"

/* price deflator*/
sort nic87code
merge nic87code using "$data\indexfinal.dta"
tab _merge
keep if _merge==3
drop _merge

g asicode88 = statecode*100 + districtcode
sort asicode88

/* generating a proper factories variable*/
drop factories
ge factories=1
